

#include <ntifs.h>
#include "common.h"

#include "winver.h"

typedef 
NTSTATUS 
(NTAPI * PFN_RtlGetVersion)(
    OUT PRTL_OSVERSIONINFOW lpVersionInformation
);

WINVER_ENUM gWinVerion = WINDOWS_VER_UNKNOWN;

WINVER_ENUM 
GetWindowsVersion(
    VOID
)
/*++

Routine Description:

    get the windows version. copy from internet.

Arguments:

	None

Return Value:

	None

--*/
{
	UNICODE_STRING ustrFuncName = { 0 }; 
	RTL_OSVERSIONINFOEXW osverinfo = { sizeof(osverinfo) }; 
	PFN_RtlGetVersion pfnRtlGetVersion = NULL;

	if (gWinVerion)
		return gWinVerion;
    
    //
    //  try to get the RtlGetVersion if error use PsGetVersion instead
    //
    
	RtlInitUnicodeString(&ustrFuncName, L"RtlGetVersion"); 
	pfnRtlGetVersion = MmGetSystemRoutineAddress(&ustrFuncName); 

	if (pfnRtlGetVersion){
		pfnRtlGetVersion((PRTL_OSVERSIONINFOW)&osverinfo); 
	}else{
        
        //
        // PsGetVersion is obsolete in Windows XP and later versions of the 
        // Windows operating system. Use RtlGetVersion instead.
        //
        
		PsGetVersion(&osverinfo.dwMajorVersion, &osverinfo.dwMinorVersion, 
                     &osverinfo.dwBuildNumber, NULL);
	}
    
    switch(osverinfo.dwMajorVersion){
    case 5:
        {
            if (osverinfo.dwMinorVersion == 0){
                gWinVerion = WINDOWS_VER_2K;
            }else if(osverinfo.dwMinorVersion == 1){
                gWinVerion = WINDOWS_VER_XP;
            }else if(osverinfo.dwMinorVersion == 2){
                if (osverinfo.dwMinorVersion == 0){
                    gWinVerion = WINDOWS_VER_2K3;
                }else{
                    gWinVerion = WINDOWS_VER_2K3_SPX;
                }
            }
        }
        break;
    case 6:
        {
            if (osverinfo.dwMinorVersion == 0){
                gWinVerion = WINDOWS_VER_VISTA;
            }else if(osverinfo.dwMinorVersion == 1){
                gWinVerion = WINDOWS_VER_7;
            }
        }
        break;
    default:
        gWinVerion = WINDOWS_VER_UNKNOWN;
        break;
    }

	return gWinVerion;
}


BOOLEAN
IsWindowsXpTo2003(
    VOID
)
{
    WINVER_ENUM Version = GetWindowsVersion();

    if (Version >= WINDOWS_VER_XP && Version <= WINDOWS_VER_2K3_SPX){
        return TRUE;
    }
    return FALSE;
}

BOOLEAN
IsWindowsVistaToWin7(
    VOID
)
{
    WINVER_ENUM Version = GetWindowsVersion();
    if (Version == WINDOWS_VER_VISTA || Version == WINDOWS_VER_7){
        return TRUE;
    }
    return FALSE;
}

BOOLEAN
IsWindows2K(
    VOID
)
{
    return (GetWindowsVersion() == WINDOWS_VER_2K);
}